package gat.api.rest.background.tg;

import com.bitsun.core.common.persistence.Pager;
import com.bitsun.core.common.web.ResultDTO;
import gat.api.background.tg.AccountDbApi;
import gat.api.config.annotation.CheckPermissions;
import gat.application.tg.AccountDbService;
import gat.dto.req.tg.AccountDbReqDto;
import gat.dto.req.tg.AccountDbReqParams;
import gat.dto.req.tg.BindAccountParams;
import gat.dto.req.tg.FreezeAccountParams;
import gat.dto.res.tg.AccountDbResDto;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

/**
 * 账号
 * @Author: yanshukeji-java
 * @Email: yanshukeji-java
 */

@Slf4j
@SuppressWarnings("unchecked")
@RestController
@RequestMapping("/accountDb")
public class AccountDbApiController implements AccountDbApi {


    @Autowired
    private AccountDbService accountDbService;

    /**
     * 保存账号
     * @param reqDto
     * @return
     */
    @CheckPermissions(value = "accountDb:save")
    @Override
    @PostMapping(consumes = {"application/json"}, produces = {"application/json"})
    public ResultDTO<AccountDbResDto> addAccountDb(
        @ApiParam(value = "要保存的对象", required = true) @Valid @RequestBody AccountDbReqDto reqDto) {
        AccountDbResDto resDto = accountDbService.save(reqDto);
        return ResultDTO.ok(resDto);
    }

    /**
     * 删除账号
     * @param ids 用英文逗号隔开
     * @return
     */
    @CheckPermissions(value = "accountDb:deleted")
    @Override
    @DeleteMapping(value = "/{ids}", produces = {"application/json"})
    public ResultDTO<Void> deleteAccountDb(
        @ApiParam(value = "要删除的对象主键，多个对象主键可以用英文逗号隔开", required = true) @PathVariable("ids") String ids) {
        boolean bl = accountDbService.deleteByIds(ids);
        return ResultDTO.ok();
    }


    /**
     * 根据id查询
     * @param id
     * @return
     */
    @CheckPermissions(value = "accountDb:getOne")
    @Override
    @GetMapping(value = "/{id}", produces = {"application/json"})
    public ResultDTO<AccountDbResDto> getOneAccountDb(@ApiParam(value = "要查询的对象主键", required = true) @PathVariable("id") Long id) {
        AccountDbResDto resDto = accountDbService.selectOne(id);
        return ResultDTO.ok(resDto);
    }

    /**
     * 更新账号
     * @param id
     * @param reqDto
     * @return
     */
    @CheckPermissions(value = "accountDb:update")
    @Override
    @PatchMapping(value = "/{id}", produces = {"application/json"})
    public ResultDTO<AccountDbResDto> patchUpdateAccountDb(@ApiParam(value = "对象ID", required = true) @PathVariable("id") Long id,
        @ApiParam(value = "要修改的对象，对象属性有值的才更新,即null值不更新", required = true) @Valid @RequestBody AccountDbReqDto reqDto) {
        accountDbService.updateProps(id, reqDto);
        AccountDbResDto resDto = accountDbService.selectOne(id);
        return ResultDTO.ok(resDto);
    }

    /**
     * 分页查询账号
     * @param params 参数params
     * @return
     */
    @CheckPermissions(value = "accountDb:queryAccountDb")
    @Override
    @GetMapping(produces = {"application/json"})
    public ResultDTO<Pager<AccountDbResDto>> queryAccountDb(AccountDbReqParams params) {
        Pager<AccountDbResDto> resDtoPager = accountDbService.queryAccountDb(params);
        return ResultDTO.ok(resDtoPager);
    }

    // 导入账号

    /**
     * 导入账号
     * @param file
     * @return
     */
    @CheckPermissions(value = "accountDb:importAccount")
    @Override
    @PostMapping("/importAccount")
    public ResultDTO<Void> importAccount(@RequestParam("file") MultipartFile file) {
        accountDbService.importAccount(file);
        return ResultDTO.ok();
    }

    // 导入账号

    /**
     * 设置折扣
     * @param reqDto
     * @return
     */
    @CheckPermissions(value = "accountDb:setDiscount")
    @PostMapping("/setDiscount")
    public ResultDTO<Void> setDiscount(@RequestBody AccountDbReqDto reqDto) {
        accountDbService.setDiscount(reqDto);
        return ResultDTO.ok();
    }

    // 冻结账号  不做解冻功能

    /**
     * 冻结账号
     * @param freezeAccountParams
     * @return
     */
    @CheckPermissions(value = "accountDb:freezeAccount")
    @PostMapping("/freezeAccount")
    public ResultDTO<Void> freezeAccount(@RequestBody FreezeAccountParams freezeAccountParams) {
        accountDbService.freezeAccount(freezeAccountParams);
        return ResultDTO.ok();
    }

    // 分配api账号
    // 与某个机器人进行绑定

    /**
     * 分配api账号
     * @param bindAccountParams
     * @return
     */
    @CheckPermissions(value = "accountDb:bindBot")
    @PostMapping("/bindBot")
    public ResultDTO<Void> bindBot(@RequestBody BindAccountParams bindAccountParams) {
        accountDbService.bindBot(bindAccountParams);
        return ResultDTO.ok();
    }

    // 全量同步api账号

    @PostMapping("/downCloudAccount")
    public ResultDTO<Void> downCloudAccount() {
        accountDbService.downCloudAccount();
        return ResultDTO.ok();
    }

    @PostMapping("/mappingBot")
    public ResultDTO<Void> mappingBot(@RequestParam("file") MultipartFile file) {
        accountDbService.mappingBot(file);
        return ResultDTO.ok();
    }

}
